package net.xdclass.controller;


import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.request.UserLoginRequest;
import net.xdclass.request.UserRegisterRequest;
import net.xdclass.service.FileService;
import net.xdclass.service.UserService;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guangyang
 * @since 2023-01-09
 */
@RestController
@RequestMapping("/api/user/v1/")
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    /**
     *  上传用户头像
     *
     *  默认最大1M，超过则报错
     *
     * @param file 文件
     * @return 存放路径
     */
    @PostMapping(value = "upload")
    @ApiParam("用户头像上传")
    public JsonData uploadUserImg(
            @ApiParam(value = "文件上传",required = true)
            @RequestPart("file") MultipartFile file){

        String result = fileService.uploadUserImg(file);

        return result!=null?JsonData.buildSuccess(result):JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }
    /**
     *用户注册
     */
    @PostMapping("register")
    @ApiOperation("用户注册")
    public JsonData register(
            @ApiParam("用户注册对象")
            @RequestBody UserRegisterRequest request){

        JsonData register = userService.register(request);
        return register;

    }

    @PostMapping("login")
    @ApiOperation("用户登录接口")
    public JsonData login(@ApiParam("用户登录对象") @RequestBody UserLoginRequest userLoginRequest){
        return  userService.login(userLoginRequest);
    }

    @GetMapping("detail")
    @ApiOperation("用户信息接口")
    public JsonData detail(){
        return userService.detail();
    }


}

